package com.agann.util;

public class RS_Util extends Util{
	
	public RS_Util(int n) {
		super(n);
	}
	
	
	public void update(Double num) {
		if (history.size() < n) {
			history.add(num);
		} else if (history.size() == n) {
			history.add(num);
			score = getUpAvg() / getDownAvg();
		} else {
			history.pollFirst();
			history.add(num);
			score = getUpAvg() / getDownAvg();
		}
	}
	
	public double getUpAvg() {
		double rtn = 0;
		for (int i = 1; i <= n; i++) {
			double t = history.get(i)-history.get(i-1);
			if (t > 0) {
				rtn += t/n;
			}
		}
		return rtn;
	}
	
	public double getDownAvg() {
		double rtn = 0;
		for (int i = 1; i <= n; i++) {
			double t = history.get(i)-history.get(i-1);
			if (t < 0) {
				rtn += -t/n;
			}
		}
		return rtn;
	}

}
